package com.bdq.basic.application.service.impl;

import cn.hutool.core.io.FileUtil;
import com.bdq.basic.application.model.cmd.FileUploadCmd;
import com.bdq.basic.application.service.FileAppCmdService;
import com.bdq.basic.infrastructure.enums.FileEnum;
import com.bdq.basic.infrastructure.model.dto.FileUploadInfraDTO;
import com.bdq.basic.infrastructure.util.FileUploadUtil;
import com.bdq.common.utils.StaticFileUtil;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author malei
 * @date 2024-05-02 14:50
 */
@Service
public class FileAppCmdServiceImpl implements FileAppCmdService {

    @Override
    public String uploadFile(FileUploadCmd cmd) throws IOException {
        // 1 文件上传 返回相对路径
        FileUploadInfraDTO infraDTO = new FileUploadInfraDTO();
        infraDTO.setFileEnum(FileEnum.getByBizType(cmd.getBizType()));
        infraDTO.setContent(cmd.getFile().getBytes());
        infraDTO.setSuffix(FileUtil.getSuffix(cmd.getFile().getOriginalFilename()));
        String relativePath = FileUploadUtil.save(infraDTO);

        // 2 静态资源域名
        return StaticFileUtil.domainConcat(relativePath);
    }
}
